//
//  HMUserAccount.swift
//  weibo28
//
//  Created by 范英强 on 16/3/25.
//  Copyright © 2016年 范英强. All rights reserved.
//

import UIKit

class HMUserAccount: NSObject,NSCoding {
    
    // 用于调用access_token，接口获取授权后的access token
    var access_token: String?
    // access_token的生命周期，单位是秒数
    // 普通的用户的 过期时间三天  开发者账号 过期时间5年
    var expires_in: NSTimeInterval = 0{
        didSet{
           expiresDate = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    // 获取到当前accessToken 时候的返回来一个过期的秒数 + 当前日期
    var expiresDate: NSDate?
    // 当前授权用户的UID
    var uid: String?
    // 用户昵称
    var screen_name:String?
    // 用户头像
    var avatar_large: String?
    
    init(dict:[String:AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
    }
    
   // 重写 description
    override var description: String {
        let key = ["access_token", "expires_in", "uid", "screen_name", "avatar_large"]
        return dictionaryWithValuesForKeys(key).description
    }
    
    
    // 01 归档
    // 把对象转成文件
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(expiresDate, forKey: "expiresDate")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
    }
    
        // 02 解档
    // 从文件中得到二进制文件
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
        uid = aDecoder.decodeObjectForKey("uid") as? String
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
    }
    
    
    
}
